#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @who:     spacelacc
# @when:    2025/04/26
# @what:    Widget for plotting probe data
# @vers:    V1


from matplotlib                         import pyplot
from matplotlib.figure                  import Figure
from matplotlib.backends.backend_qtagg  import FigureCanvas

from PySide6.QtWidgets import QWidget, QApplication
from PySide6.QtWidgets import QVBoxLayout, QSizePolicy


class CustomDataPlot(QWidget):
    def __init__(self, width, height, adapter, parent=None):
        super().__init__(parent)
        self.mww = width        # Main Window Width
        self.mwh = height       # Main Window Height
        self.adapter = adapter
        self.figure = pyplot.Figure()
        self.canvas = FigureCanvas(self.figure)
        self.canvas.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.canvas.updateGeometry()

        ctplotter = QVBoxLayout(self)
        ctplotter.addWidget(self.canvas)
        ctplotter.setContentsMargins(0, 0, 0, 0)

        self.xticks = [f't{i}' for i in range(1, 9)]
        pyplot.xticks(range(1,9), self.xticks)

    def plot(self, data):
        # Checking for data
        if not data:
            return

        self.cache = data
        self.figure.clear()
        self.axes = self.figure.subplots()

        for d in data:
            self.axes.plot(self.xticks, d[5:-4], label=d[1],
                           linewidth=2, marker='o')

        self.axes.legend()
        self.canvas.draw()




class ProbePlotter(QWidget):
    def __init__(self, adapter):
        super().__init__()

        self.cache = []
        self.linewidth = adapter.getint('probeplotter', 'linewidth', fallback=2)
        self.linecolor = adapter.get('probeplotter', 'linecolor', fallback='#008fbf')
        self.linestyle = adapter.get('probeplotter', 'linestyle', fallback='solid')
        self.markersize = adapter.getint('probeplotter', 'markersize', fallback=6)
        self.markershape = adapter.get('probeplotter', 'markershape', fallback='d')
        self.markerfacecolor = adapter.get('probeplotter', 'markerfacecolor', fallback='#008fbf')
        self.markeredgecolor = adapter.get('probeplotter', 'markeredgecolor', fallback='#008fbf')
        self.subplotfacecolor = adapter.get('probeplotter', 'subplotfacecolor', fallback='#cccccc')
        self.legendedgecolor = adapter.get('probeplotter', 'legendegecolor', fallback='b')
        self.legendfacecolor = adapter.get('probeplotter', 'legendfacecolor', fallback='y')

        self.xticks = [f't{i}' for i in range(1, 9)]
        pyplot.xticks(range(1,9), self.xticks, rotation=45)


        self.figure = pyplot.Figure()
        self.canvas = FigureCanvas(self.figure)
        self.canvas.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.canvas.updateGeometry()

#       self.xticks = [f't{i}' for i in range(1, 9)]
#       pyplot.xticks(range(1,9), self.xticks, rotation=45)

        ctPlotter = QVBoxLayout()
        ctPlotter.setContentsMargins(0, 0, 0, 0)
        ctPlotter.addWidget(self.canvas)
        self.setLayout(ctPlotter)

    def plot1(self):
        self.figure.clear()
        self.axes = self.figure.subplots()
        self.axes.plot([1, 2, 3, 4])
        self.canvas.draw()

    def plot(self, data):
        if not data:
            return

        self.cache = data
        self.figure.clear()
#       self.axes = self.figure.subplots(facecolor=self.subplotfacecolor)
        self.axes = self.figure.subplots()
        for d in self.cache:
            self.axes.plot(self.xticks, d[5:-4],
                            color=self.linecolor,
                            marker=self.markershape,
                            markersize=self.markersize,
                            markerfacecolor=self.markerfacecolor,
                            markeredgecolor=self.markeredgecolor,
                            linestyle=self.linestyle,
                            linewidth=self.linewidth,
                            label=d[0])
        self.axes.legend(edgecolor=self.legendedgecolor,
                         facecolor=self.legendfacecolor)
